package cn.zn.modules.zzsys.cur.service.impl;

import cn.zn.framework.common.exception.RRException;
import cn.zn.framework.common.utils.PageTUtils;
import cn.zn.framework.common.utils.PageUtils;
import cn.zn.modules.zzsys.cur.mapper.TSysUserMapper;
import cn.zn.modules.zzsys.cur.entity.TMemberInfo;
import cn.zn.modules.zzsys.cur.entity.TSysUserEntity;
import cn.zn.modules.zzsys.inter.UserInter;
import cn.zn.modules.zzsys.inter.UserRoleInter;
import cn.zn.modules.zzsys.cur.service.TMemberInfoService;
import cn.zn.modules.zzsys.cur.service.TSysUserService;
import cn.zn.modules.zzsys.inter.dto.UserRegisterDTO;
import cn.zn.modules.zzsys.inter.vo.SysUserVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;


@Service("tSysUserService")
@Slf4j
public class TSysUserServiceImpl extends ServiceImpl<TSysUserMapper, TSysUserEntity> implements TSysUserService, UserRoleInter, UserInter {

    @Autowired
    private TMemberInfoService tMemberInfoService;

    @Override
    public PageUtils queryPage(TSysUserEntity tSysUser, IPage<TSysUserEntity> page) {

        QueryWrapper<TSysUserEntity> tSysUserQueryWrapper = Wrappers.query();

        IPage<TSysUserEntity> pageResult = this.page(page, tSysUserQueryWrapper);
        return new PageUtils(pageResult);
    }

    @Override
    public TSysUserEntity saveForReg(String username, String password, String nickName,
                                     String headimgurl, String mobile, Integer gender, String teacherId, String deptId) {
        TSysUserEntity mExist = findByUsername(username);
        if (mExist != null) {
            throw new RRException("用户已存在!");
        }
        TSysUserEntity m = new TSysUserEntity();
        m.setUsername(username);
        m.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        m.setSortNum(100);
        m.setTTeacherInfoId(teacherId);
        m.setTSysDeptId(deptId);
        m.setIsSys(false);
        super.save(m);
        // 添加默认角色
        this.baseMapper.insertUserRole(m.getId(), "bbc6405e5e484d0b015e484d47f60005");
        // 保存会员信息
        TMemberInfo tMemberInfo = new TMemberInfo();
        tMemberInfo.setTSysUserId(m.getId());
        tMemberInfo.setName(nickName);
        tMemberInfoService.save(tMemberInfo);
        return m;
    }

    /**
     * 根据用户名查询
     *
     * @param username
     * @return
     */
    public TSysUserEntity findByUsername(String username) {
        return super.getOne(new QueryWrapper<TSysUserEntity>().eq("USERNAME_", username));
    }

    @Override
    public Boolean hasRole(String loginUserId, String roleCode) {
        return this.baseMapper.hasRole(loginUserId, roleCode) > 0;
    }

    @Override
    public PageTUtils<SysUserVO> queryPageByRole(Long roleId, List<String> unUserCode, long pageNo, long pageSize) {
        return null;
    }

    @Override
    public List<SysUserVO> queryByUserCodes(List<String> userCodes) {
        return null;
    }

    @Override
    public SysUserVO queryByWappId(Long wappId) {
        return null;
    }

    @Override
    public SysUserVO queryByLoginUserId(Long loginUserId) {
        return null;
    }

    @Override
    public SysUserVO queryByLoginUserId2(Long loginUserId) {
        return null;
    }

    @Override
    public List<SysUserVO> queryByIds(List<Long> userIds) {
        return null;
    }

    @Override
    public List<SysUserVO> queryByTenantAndRole(Long tenantId, Long roleId) {
        return null;
    }

    @Override
    public void saveUserForRegister(UserRegisterDTO userRegisterDTO, Long tenantId) {

    }

    @Override
    public Boolean isAdminForTanent(Long userId, Long tenantId) {
        return null;
    }
}